<template>
	<view>
		<view class="x-c">
			
			<image class="logo-bg" :src='this.commonImg() + "login_bg.jpg"' mode=""></image>
		</view>
		<view class="logo">
			<view class="img">
				欢迎，登录
				<!-- <image :src='this.commonImg() + "logob.png"' mode="widthFix"></image> -->
			</view>
		</view>
		<view class="modal-content">


			<view class="box-btn">
				<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
					点击登录
				</button>
			

				<button @tap='noBtn' style="background: #ccc;color: #333">
					暂不登录
				</button>
				
			<!-- 	<button open-type="launchApp" app-parameter="dataStr" @tap="goBack">
					返回App
				</button> -->
			</view>


		</view>
	</view>
</template>

<script>
	var _self;
	export default {
		data() {
			return {
				editModal: false,
				showInfo: false,
				userInfo: {
					avatar: '',
					nickname: ''
				},
				name: '',
				passwd: '',
				code: '',
				isShow: true,
				pid: '',
				path: '',
				dataStr: null,
				transitiveData: null
			}
		},

		onLoad: function(options) {
			console.log(options, 'login')
			this.transitiveData = options.res
			
			this.dataStr = JSON.stringify({
				data: 'MsgBackToApp',
				transitiveData: this.transitiveData
			})
			if (options.path) {
				this.path = options.path
			}
			let that = this;

			// if(options.query.pid||uni.getStorageSync("share_pid")){
			// 	const scene = options.query.pid
			// 	that.pid=scene
			// 	uni.setStorageSync("share_pid", scene);
			// 	return false
			// }else{
			// 	const scene = options.scene
			// 	uni.setStorageSync("share_pid", scene);
			// }
		},
		methods: {
			getPhoneNumber: function(e) {
				console.log(e)
				console.log(uni.getStorageSync("share_pid"), '登录')
				let that = this
				this.isShow = false
				var commonUrl = this.websiteUrl()
				uni.showLoading({
					title: '登录中'
				})

				// #ifdef MP-WEIXIN	
				uni.login({
					success: function(res) {
						console.log(res)
						var code = res.code;
						if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
							uni.showToast({
								title: '取消授权',
								icon: "error"
							});
						} else {
							var encryptedData = e.detail.encryptedData;
							var iv = e.detail.iv;
							let url = ''

							// // #ifdef MP-TOUTIAO 
							// url = commonUrl + "dy-login"
							// // #endif


							url = commonUrl + "login"


							uni.request({
								url: url,
								data: {
									iv: iv,
									encrypt_data: encryptedData,
									code: code,
									bind_code: e.detail.code,
									pid: uni.getStorageSync("share_pid"),
								},
								method: 'POST',
								success: res => {
									if (res.data.code == 1) {
										console.log("login: " + res.data.data.token);
										uni.setStorageSync("token", res.data.data.token)

										wx.getSetting({
											withSubscriptions: true,
											success(res) {

												if (Object.keys(res
														.subscriptionsSetting)
													.includes(
														'7NbROvjKSTCm4_GHb_8PIp6eCTxRntK1tpcmoya-3w4'
													) == 'accept') {
													if (that.path ==
														'/pages/tabBar/home/home' ||
														that
														.path ==
														'/pages/tabBar/user/user' ||
														that.path ==
														'/pages/tabBar/order/add'
													) {
														uni.switchTab({
															url: that.path ?
																that
																.path :
																'/pages/tabBar/home/home'
														})
													} else {
														if (that.path) {
															console.log(4444)
															uni.navigateBack({
																delta: 1 // delta值为1时表示返回的页面层数
															});
														} else {
															uni.switchTab({
																url: '/pages/tabBar/home/home'
															})
														}
													}
												} else {
													uni.showModal({
														title: '温馨提示',
														content: '为更好为您服务，服务号需要在您下单时向您发送消息',
														showCancel: true,
														cancelText: '拒绝',
														confirmText: '同意',
														success: function(
															res) {
															if (res
																.confirm) {
																uni.requestSubscribeMessage({
																	tmplIds: [
																		'7NbROvjKSTCm4_GHb_8PIp6eCTxRntK1tpcmoya-3w4',
																		'yyGySmR4uIzKlQJQjgDqSK5LU2YxHL9DUTA31Ldb-MU'
																	],
																	success(
																		res
																	) {

																		if (that
																			.path ==
																			'/pages/tabBar/home/home' ||
																			that
																			.path ==
																			'/pages/tabBar/user/user' ||
																			that
																			.path ==
																			'/pages/tabBar/order/add'
																		) {
																			uni.switchTab({
																				url: that
																					.path ?
																					that
																					.path :
																					'/pages/tabBar/home/home'
																			})
																		} else {
																			if (that
																				.path
																			) {
																				console
																					.log(
																						4444
																					)
																				uni.navigateBack({
																					delta: 1 // delta值为1时表示返回的页面层数
																				});
																			} else {
																				uni.switchTab({
																					url: '/pages/tabBar/home/home'
																				})
																			}
																		}


																	},
																	fail(
																		err
																	) {
																		if (that
																			.path ==
																			'/pages/tabBar/home/home' ||
																			that
																			.path ==
																			'/pages/tabBar/user/user' ||
																			that
																			.path ==
																			'/pages/tabBar/order/add'
																		) {
																			uni.switchTab({
																				url: that
																					.path ?
																					that
																					.path :
																					'/pages/tabBar/home/home'
																			})
																		} else {
																			if (that
																				.path
																			) {
																				console
																					.log(
																						4444
																					)
																				uni.navigateBack({
																					delta: 1 // delta值为1时表示返回的页面层数
																				});
																			} else {
																				uni.switchTab({
																					url: '/pages/tabBar/home/home'
																				})
																			}
																		}
																	}
																});

															} else if (res
																.cancel) {
																if (that
																	.path ==
																	'/pages/tabBar/home/home' ||
																	that
																	.path ==
																	'/pages/tabBar/user/user' ||
																	that
																	.path ==
																	'/pages/tabBar/order/add'
																) {
																	uni.switchTab({
																		url: that
																			.path ?
																			that
																			.path :
																			'/pages/tabBar/home/home'
																	})
																} else {
																	if (that
																		.path
																	) {
																		console
																			.log(
																				4444
																			)
																		uni.navigateBack({
																			delta: 1 // delta值为1时表示返回的页面层数
																		});
																	} else {
																		uni.switchTab({
																			url: '/pages/tabBar/home/home'
																		})
																	}
																}
															}
														}
													});
												}

											}
										})





									}
								}
							})



						}
					},
				});
				// #endif
				uni.hideLoading();
			},

			noBtn() {
				if (this.path == '/pages/tabBar/home/home' || this.path == '/pages/tabBar/user/user' || this.path ==
					'/pages/tabBar/order/add') {

					uni.switchTab({
						url: this.path ? this.path : '/pages/tabBar/home/home'
					})
				} else {

					if (this.path) {
						uni.navigateBack({
							delta: 1 // delta值为1时表示返回的页面层数
						});
					} else {
						uni.switchTab({
							url: '/pages/tabBar/home/home'
						})
					}

					// uni.navigateTo({
					// 	url: this.path ? this.path : '/pages/tabBar/home/home'
					// })

				}
			},
			toPage() {
				uni.navigateTo({
					url: '/pages/user/yszc/yszc'
				});
			},
		}
	}
</script>

<style lang="scss">
	@import "../../static/css/login.scss";

	/* ---弹窗css--- */
	.modal-mask {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: #000;
		opacity: 0.5;
		overflow: hidden;
		color: #fff;
		z-index: 999;
	}
	.logo{color: #fff;font-weight: bold;font-size: 50rpx;}

	.logo-bg {
		margin-top: 20px;
	}

	.modal-dialog {
		width: 72%;
		position: absolute;
		top: 30%;
		left: 14%;
		background: #fff;
		border-radius: 12rpx;
		z-index: 999;
	}

	.modal-content {
		width: 100%;
		text-align: center;
		position: fixed;
		bottom: 0;
	}

	.box-btn {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 40rpx;
	}


	.iconWx {
		width: 52rpx;
		height: 41rpx;
		padding-right: 20rpx;
	}

	.iconPhone {
		width: 56rpx;
		height: 56rpx;
		padding-right: 15rpx;
	}

	.modal-content button {
		width: 80%;
		height: 80rpx;
		border-radius: 60rpx;
		font-size: 30rpx;
		color: #fff;
		background: #3cbdc5;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding: 0;
		box-sizing: border-box;
		margin: 0 20rpx;
	}

	.modal-content button::after {
		border: none;
	}
</style>